<?php

namespace Admin\Controller;
use Admin\Model\AuthGroupModel;
use Think\Page;

/**流程控制器
 * Class ArticleController
 * @package Admin\Controller
 * @author leiying
 */
class ProcessController extends AdminController {

    /**流程列表
     * @author leiying
     */
    public function processList(){
        $limit = LIMIT;
        $get = I('post.');

        $search_data = array();
        $where = array();

        if(IS_POST){
            session('searchdata_processlist',$get);
        }else{
            if(I('get.p')){
                $this->assign('p',I('get.p'));
            }else{
                session('searchdata_processlist',null);
            }
            $get = $search_data = session('searchdata_processlist');
        }
        if(isset($get['p_id']) && $get['p_id'] != ''){
            $search_data['p_id'] = $get['p_id'];
            if($get['p_id'] != 'all' ){
                $where['a.p_id'] = array('eq',$get['p_id']);
            }
        }

        if(isset($get['start_time']) && $get['start_time']){
            $search_data['start_time'] = $get['start_time'];
            $where['a.add_time'][] = array('gt',strtotime(str_replace('_', ' ', $get['start_time'])));
        }
        if(isset($get['end_time']) && $get['end_time']){
            $search_data['end_time'] = $get['end_time'];
            $where['a.add_time'][] = array('lt',strtotime(str_replace('_', ' ', $get['end_time'])));
        }


        $count = M('process')->alias('a')->where($where)->count();

        $Page = new  \Think\Page($count, $limit);// 实例化分页类 传入总记录数

        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $lists = M('process')->alias('a')->field('a.*,b.nickname,c.name project_name')
            ->join(C('DB_PREFIX').'member b on a.admin_uid = b.uid ','left')
            ->join(C('DB_PREFIX').'project c on a.p_id = c.id','left')
            ->where($where)
            ->limit($Page->firstRow.','.$Page->listRows)->order("a.add_time desc")->select();

        $this->assign('searchs',$search_data);
        $this->assign('lists',$lists);
        $projects = M('project')->order("add_time desc")->select();
        $this->assign('projects',$projects);
        $this->display();
    }

    /**添加流程
     * @author leiying
     */
    public function processAdd(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $post = I('post.');
            if(empty($post['p_id'])){
                $this->error("请选择项目分类");
            }
            $p_id = $post['p_id'];
            $data = array(
                'p_id' => $p_id,
                'content' => htmlspecialchars($post['editorValue']),
                'admin_uid' => UID,
                'add_time' => time(),
            );

            $res = M('process')->add($data);

            if($res){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }else{
            $projects = M('project')->order("add_time desc")->select();
            $this->assign('projects',$projects);
            $this->display();
        }
    }


    /**删除流程
     * @author leiying
     */
    public function processDel(){
        if(IS_POST){
            if(empty(I('post.id'))){
                $this->error("请选择要操作的数据");
            }
            $res = M('process')->where(array('id'=>I('post.id')))->delete();
            if($res){
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }else{
            $this->error('非法操作');
        }
    }

    /**编辑流程
     * @author leiying
     */
    public function processEdit(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $post = I('post.');
            if(empty($post['id'])){
                $this->error("请选择要操作的数据");
            }
            $id = $post['id'];
            if(empty($post['p_id'])){
                $this->error("请选择项目分类");
            }
            $p_id = $post['p_id'];
            $data = array(
                'p_id' => $p_id,
                'content' => htmlspecialchars($post['editorValue']),
                'admin_uid' => UID,
            );
//            $find = M("process")->where(array("p_id"=>$p_id,"id"=>array("neq",$id)))->find();
//            if($find){
//                $this->error("当前项目已经有流程了，请删除后重新添加!");
//            }

            $res = M('process')->where(array('id'=>$id))->save($data);

            if($res){
                $this->success('编辑成功');
            }else{
                $this->error('编辑失败');
            }
        }else{
            $id = I('get.id');
            if($id){
                $data = M('process')->where(array('id'=>I('get.id')))->find();
                $this->assign('data',$data);
                $projects = M('project')->order("add_time desc")->select();
                $this->assign('projects',$projects);
                $p = I('get.p',"1");
                $this->assign('p',$p);
                $this->display();
            }
        }
    }

}
